﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SharpMoney.Library
{
	public class CurrencyConverter : ICurrencyConverter
	{
		#region Variables

		private IExchangeRateProvider _exchangeRateProvider;

		#endregion

		#region Properties

		public IExchangeRateProvider ExchangeRateProvider
		{
			get
			{
				return _exchangeRateProvider;
			}
		}

		#endregion

		#region Constructors

		public CurrencyConverter( IExchangeRateProvider provider )
		{
			this._exchangeRateProvider = provider;
		}

		#endregion

		#region ICurrencyConvertor Members

		public Money Convert( Money value, Currency convertTo )
		{
			Money convertedCurrency = new Money( 0, convertTo );

			decimal toRate = this.ExchangeRateProvider.GetRate( convertTo.Code ),
				fromRate = this.ExchangeRateProvider.GetRate( value.Currency.Code );

			convertedCurrency.Value = Math.Round( value.Value * ( toRate / fromRate ), 6 );

			return convertedCurrency;
		}

		#endregion
	}
}
